package com.espirit.finance.core;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.espirit.eap.util.BigDecimalUtil;
import com.espirit.eap.util.Functions;
import com.googlecode.cswish.util.FreemarkerTplEngine;
import com.googlecode.cswish.util.InvocationUtil;
import com.googlecode.cswish.util.bean.ModelUtil;

@Service
public class AcctItemCalcService {

	@Resource
	InvocationUtil invocationUtil;
	
	@Resource
	private Functions functions;
	
	public BigDecimal getFactorAmount(AcctItem acctItem, TicketAware ticketAware, BigDecimal bigValue){
		if (acctItem == null) return bigValue;
		return getFactorAmount(acctItem.getAcctItemFactor(), ticketAware, bigValue);
	}
	
	public BigDecimal getScopeAmount(AcctItem acctItem, AcctItemScope acctItemScope, TicketAware ticketAware, BigDecimal bigValue){
		AcctItemFactor acctItemFactor = null;
		if (acctItemScope == null) {
			if (acctItem != null){
				acctItemFactor = acctItem.getAcctItemFactor();
			}
		} else {
			acctItemFactor = acctItemScope.getAcctItemFactor();
		}
		return getFactorAmount(acctItemFactor, ticketAware, bigValue);
	}
	
	private BigDecimal getFactorAmount(AcctItemFactor acctItemFactor, TicketAware ticketAware, BigDecimal bigValue){
		if (acctItemFactor == null){
			return bigValue;
		} else {
			BigDecimal factor = getFactor(acctItemFactor.getFactor(), ticketAware);
			Object factorValue = ModelUtil.getProperty(ticketAware, acctItemFactor.getProperty());
			if (factor != null && factorValue != null){
				BigDecimal bigFactorvalue = BigDecimalUtil.getBigDecimalValue(factorValue);
				return bigValue.multiply(factor).multiply(bigFactorvalue);
			} else if (factor == null && factorValue != null){
				BigDecimal bigFactorvalue = BigDecimalUtil.getBigDecimalValue(factorValue);
				if (bigFactorvalue != null && bigValue != null){
					return bigValue.multiply(bigFactorvalue);
				} else {
					return bigValue;
				}
			}  else if (factor != null && factorValue == null){
				return bigValue.multiply(factor);
			} else {
				return bigValue;
			}
		}
	}
	
	public BigDecimal getFactor(String factor, TicketAware model) {
		if (factor == null || factor.isEmpty()){
			return null;
		} else {
//			String condtion = factor.replaceAll(" > ", " gt ");
//			condtion = condtion.replaceAll(" > ", " gt ");
//			condtion = condtion.replaceAll(" < ", " lt ");
//			condtion = condtion.replaceAll(" >= ", " gte ");
//			condtion = condtion.replaceAll(" <= ", " lte ");
//			condtion = condtion.replaceAll(" and ", " && ");
//			condtion = condtion.replaceAll(" or ", " || ");
			
			Map<String, Object> parameters = new HashMap<String, Object>();
			invocationUtil.forceLoadModelAndResults(parameters);
			parameters.put("fn", functions);
			parameters.put("model", model);
//			parameters.put("action", new MapContext(action));
			
			Map<String, Object> traceResult = new HashMap<String, Object>(4);
			traceResult.put("result", null);

			FreemarkerTplEngine.getInstance().execute(factor, parameters, traceResult);
			Object result = traceResult.get("result");
			if (result == null){
				return null;
			} else {
				return BigDecimalUtil.getBigDecimalValue(result);
			}
		}
	}
}
